package org.linlinjava.litemall.wx.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.JsonResponse;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.dto.SubmitOrderDTO;
import org.linlinjava.litemall.wx.service.WxOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/wx/order")
@Validated
@Api("订单服务")
public class WxOrderController {
    private final Log logger = LogFactory.getLog(WxOrderController.class);

    @Autowired
    private WxOrderService wxOrderService;

    /**
     * 订单列表
     *
     * @param userId   用户ID
     * @param showType 显示类型，如果是0则是全部订单
     * @param page     分页页数
     * @param limit     分页大小
     * @param sort     排序字段
     * @param order     排序方式
     * @return 订单列表
     */
    @GetMapping("list")
    @ApiOperation("订单列表")
    public Object list(@LoginUser Integer userId,
                       @RequestParam(defaultValue = "0") Integer showType,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        return wxOrderService.list(userId, showType, page, limit, sort, order);
    }

    /**
     * 我的订单数量
     *
     * @param userId  用户ID
     * @return 我的订单数量
     */
    @GetMapping("my")
    @ApiOperation("我的订单数量")
    public Object myOrderNum(@LoginUser Integer userId) {
        return wxOrderService.myOrderNum(userId);
    }

    /**
     * 订单详情
     *
     * @param userId  用户ID
     * @param orderId 订单ID
     * @return 订单详情
     */
    @GetMapping("detail")
    @ApiOperation("订单详情")
    public Object detail(@LoginUser Integer userId, @NotNull Integer orderId) {
        return wxOrderService.detail(userId, orderId);
    }

    /**
     * 物流详情
     *
     * @param orderId 订单ID
     * @param userId  用户id
     * @return 订单详情
     */
    @GetMapping("express")
    @ApiOperation("物流详情")
    public JsonResponse express(@LoginUser Integer userId, Integer orderId) {
        if (userId == null) {
            return JsonResponse.error("请登录");
        }
        return wxOrderService.expressInfo(orderId);
    }

    /**
     * 提交订单
     *
     * @param submitOrderDTO   订单信息，{ cartId：xxx, addressId: xxx, couponId: xxx, message: xxx, grouponRulesId: xxx,  grouponLinkId: xxx}
     * @return 提交订单操作结果
     */
    @PostMapping("/submit")
    @ApiOperation("提交订单")
    public JsonResponse submit(@LoginUser @RequestBody SubmitOrderDTO submitOrderDTO) {
        return wxOrderService.submit(submitOrderDTO);
    }

    /**
     * 立即购买
     *
     * @param submitOrderDTO   订单信息，{ cartId：xxx, addressId: xxx, couponId: xxx, message: xxx, grouponRulesId: xxx,  grouponLinkId: xxx}
     * @return 提交订单操作结果
     */
    @PostMapping("buy/now")
    @ApiOperation("立即购买")
    public JsonResponse buyNow(@LoginUser @RequestBody SubmitOrderDTO submitOrderDTO) {
        return wxOrderService.buyNow(submitOrderDTO);
    }

    /**
     * 取消订单
     *
     * @param userId 用户ID
     * @param orderId   订单id，{ orderId：xxx }
     * @return 取消订单操作结果
     */
    @PostMapping("cancel")
    @ApiOperation("取消订单")
    public JsonResponse cancel(@LoginUser Integer userId, Integer orderId) {
        return wxOrderService.cancel(userId, orderId);
    }

    /**
     * 付款订单的预支付会话标识
     *
     * @param userId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 支付订单ID
     */
    @PostMapping("prepay")
    @ApiOperation("付款订单的预支付会话标识")
    public Object prepay(@LoginUser Integer userId, @RequestBody String body, HttpServletRequest request) {
        return wxOrderService.prepay(userId, body, request);
    }

    /**
     * 微信H5支付
     * @param userId
     * @param orderId
     * @param request
     * @return
     */
    @PostMapping("h5pay")
    @ApiOperation("微信H5支付")
    public Object h5pay(@LoginUser Integer userId, Integer orderId, HttpServletRequest request) {
        return wxOrderService.h5pay(userId, orderId, request);
    }

    /**
     * 小程序微信支付
     * @return
     * @throws Exception
     */
    @ApiOperation("小程序微信支付")
    @GetMapping("/pay")
    public JsonResponse wxPayFunction(@LoginUser Integer userId, Integer orderId) throws Exception{
        return JsonResponse.ok(wxOrderService.wxPay(userId, orderId));
    }


    /**
     * 微信付款成功或失败回调接口
     * <p>
     *  TODO
     *  注意，这里pay-notify是示例地址，建议开发者应该设立一个隐蔽的回调地址
     *
     * @param request 请求内容
     * @param response 响应内容
     * @return 操作结果
     */
    @PostMapping("pay-notify")
    @ApiOperation("微信付款成功或失败回调接口")
    public Object payNotify(HttpServletRequest request, HttpServletResponse response) {
        return wxOrderService.payNotify(request, response);
    }

    /**
     * 订单申请退款
     *
     * @param userId 用户ID
     * @param orderId   订单Id
     * @return 订单退款操作结果
     */
    @GetMapping("refund")
    @ApiOperation("订单申请退款")
    public Object refund(@LoginUser Integer userId, Integer orderId) {
        return wxOrderService.refund(userId, orderId);
    }

    /**
     * 取消退款
     *
     * @param userId 用户ID
     * @param orderId   订单Id
     * @return 订单取消退款操作结果
     */
    @GetMapping("cancelRefund")
    @ApiOperation("取消退款")
    public Object cancelRefund(@LoginUser Integer userId, Integer orderId) {
        return wxOrderService.cancelRefund(userId, orderId);
    }

    /**
     * 确认收货
     *
     * @param userId 用户ID
     * @param orderId   订单信息，{ orderId：xxx }
     * @return 订单操作结果
     */
    @GetMapping("confirm")
    @ApiOperation("确认收货")
    public Object confirm(@LoginUser Integer userId, Integer orderId) {
        return wxOrderService.confirm(userId, orderId);
    }

    /**
     * 删除订单
     *
     * @param userId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 订单操作结果
     */
    @PostMapping("delete")
    @ApiOperation("删除订单")
    public Object delete(@LoginUser Integer userId, @RequestBody String body) {
        return wxOrderService.delete(userId, body);
    }

    /**
     * 再来一单
     *
     * @param userId 用户ID
     * @param orderId   订单Id
     * @return 订单操作结果
     */
    @GetMapping("encore")
    @ApiOperation("再来一单")
    public JsonResponse delete(@LoginUser Integer userId,  Integer orderId) {
        return wxOrderService.encore(userId, orderId);
    }

    /**
     * 待评价订单商品信息
     *
     * @param userId  用户ID
     * @param orderId 订单ID
     * @param goodsId 商品ID
     * @return 待评价订单商品信息
     */
    @GetMapping("goods")
    @ApiOperation("待评价订单商品信息")
    public Object goods(@LoginUser Integer userId,
                        @NotNull Integer orderId,
                        @NotNull Integer goodsId) {
        return wxOrderService.goods(userId, orderId, goodsId);
    }

    /**
     * 评价订单商品
     *
     * @param userId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 订单操作结果
     */
    @PostMapping("comment")
    @ApiOperation("评价订单商品")
    public Object comment(@LoginUser Integer userId, @RequestBody String body) {
        return wxOrderService.comment(userId, body);
    }

}
